package com.jiukuaidao.zhulin.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiukuaidao.library.base.BaseHolder;
import com.jiukuaidao.library.base.BaseXrecyclerViewAdapter;
import com.jiukuaidao.library.widget.recyclerview.XRecyclerView;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.entity.ApiOrderList;
import com.jiukuaidao.zhulin.module.order.EventClickOrder;
import com.jiukuaidao.zhulin.module.order.OrderState;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/7
 * Description :
 */
public class OrderListAdapter extends BaseXrecyclerViewAdapter {
    private List<ApiOrderList.OrderList> list;

    public OrderListAdapter(Context context, int layoutId, List datas, XRecyclerView recyclerView) {
        super(context, layoutId, datas, recyclerView);
        list = datas;
    }

    @Override
    public void updateUI(BaseHolder holder, int position, Object data) {
        TextView orderTime = holder.getView(R.id.tv_order_time);
        TextView orderStatus = holder.getView(R.id.tv_status);
        LinearLayout orderPro1 = holder.getView(R.id.ll_pro1);
        LinearLayout orderPro2 = holder.getView(R.id.ll_pro2);
        TextView totalPrice = holder.getView(R.id.tv_price);
        TextView totalCount = holder.getView(R.id.tv_count);
        Button orderBtn = holder.getView(R.id.order_btn);
        TextView pro1Name = holder.getView(R.id.tv_pro1name);
        TextView pro1Count = holder.getView(R.id.tv_pro1count);
        TextView pro1Price = holder.getView(R.id.tv_pro1price);
        View proDivider = holder.getView(R.id.pro_divider);
        TextView pro2Name = holder.getView(R.id.tv_pro2name);
        TextView pro2Count = holder.getView(R.id.tv_pro2count);
        TextView pro2Price = holder.getView(R.id.tv_pro2price);
        orderTime.setText(list.get(position).createTime);
        totalPrice.setText(String.format(mContext.getString(R.string.moneySymbol), list.get(position).totalAmount));
        totalCount.setText(String.format(mContext.getString(R.string.goods_count), list.get(position).goodsTotal));
        orderStatus.setText(list.get(position).statusText);
        pro1Name.setText(list.get(position).goodsList.get(0).goodsName);
        pro1Count.setText(String.format(mContext.getString(R.string.x), list.get(position).goodsList.get(0).goodsNo));
        pro1Price.setText(String.format(mContext.getString(R.string.moneySymbol), list.get(position).goodsList.get(0).goodsPrice));

        if (list.get(position).goodsList.size() == 1) {// 如果只有一种商品
            orderPro2.setVisibility(View.GONE);
            proDivider.setVisibility(View.GONE);
        } else {// 如果大于一种商品
            orderPro2.setVisibility(View.VISIBLE);
            proDivider.setVisibility(View.VISIBLE);
            pro2Name.setText(list.get(position).goodsList.get(1).goodsName);
            pro2Count.setText(String.format(mContext.getString(R.string.x), list.get(position).goodsList.get(1).goodsNo));
            pro2Price.setText(String.format(mContext.getString(R.string.moneySymbol), list.get(position).goodsList.get(1).goodsPrice));
        }

        setOrderBtn(position, orderBtn);
    }

    @Override
    protected void onItemClick(BaseHolder holder, int position) {
        super.onItemClick(holder, position);
        EventClickOrder clickOrder = new EventClickOrder();
        clickOrder.mOrderList = list.get(position);
        clickOrder.isButton = false;
        EventBus.getDefault().post(clickOrder);
    }

    public void setOrderBtn(final int position, Button btn) {
        btn.setVisibility(View.GONE);
        int status = list.get(position).status;
        switch (status) {
            case OrderState.ORDER_UNPAID: //去支付
                btn.setText("去支付");
                btn.setVisibility(View.VISIBLE);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EventClickOrder clickOrder = new EventClickOrder();
                        clickOrder.mOrderList = list.get(position);
                        clickOrder.isButton = true;
                        EventBus.getDefault().post(clickOrder);
                    }
                });
                break;
            case OrderState.ORDER_SHIP: //签收
                btn.setText("签收");
                btn.setVisibility(View.VISIBLE);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EventClickOrder clickOrder = new EventClickOrder();
                        clickOrder.mOrderList = list.get(position);
                        clickOrder.isButton = true;
                        EventBus.getDefault().post(clickOrder);
                    }
                });
                break;
            case OrderState.ORDER_RECEIVED: //评价
                btn.setText("评价");
                btn.setVisibility(View.VISIBLE);
                btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EventClickOrder clickOrder = new EventClickOrder();
                        clickOrder.mOrderList = list.get(position);
                        clickOrder.isButton = true;
                        EventBus.getDefault().post(clickOrder);
                    }
                });
                break;

        }
    }

}
